package com.unience.ec2.eclipse.view;

import java.util.Collection;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.time.Second;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.experimental.chart.swt.ChartComposite;

import com.unience.aws.monitor.AwsMonitorThread;
import com.unience.aws.monitor.Ec2InstancesListener;
import com.unience.aws.monitor.model.MonitorEc2Instance;
import com.unience.ec2.eclipse.config.Config;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class Ec2MonitorViewPart extends ViewPart  implements Ec2InstancesListener {
    public static final String ID_VIEW =
        "com.unience.ec2.eclipse.view.Ec2MonitorViewPart"; //$NON-NLS-1$

    private static Log log = LogFactory.getLog(Ec2MonitorViewPart.class);
    ChartComposite composite1;
    
    JFreeChart chart;
    
   // UpdateEc2InstanceThread updater;
    
	protected AwsMonitorThread updater;
    private TimeSeries series;
    
    Ec2MonitorViewPart monitor;
    
    int numInstances = 0;
    
    /**
     * 
     */
    public Ec2MonitorViewPart() {
        super();        
    }

    public void update(final Collection<MonitorEc2Instance> instancesList, final Object listener) {
		
    
    	monitor = this;
    	
		if (instancesList != null){
			numInstances =  instancesList.size();
		}
		else{
			numInstances = 0;
		}
		
		 Config.getInstance().getDisplay().asyncExec(new Runnable() {
			  public void run(){
			    	
			    	final IWorkbench workbench = PlatformUI.getWorkbench();
					if (workbench == null)
						return;
					final Display display = workbench.getDisplay();
					
					if (!display.isDisposed()){
				    	log.info("updating chart numInstances " + numInstances);
				    	monitor.getSeries().addOrUpdate(new Second(), numInstances);
					}
			      }
			    });
			  
		
	}
    
    /* (non-Javadoc)
     * @see org.eclipse.ui.IWorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
     */
    public void createPartControl(Composite parent) {
    	{
	    	parent.setSize(781, 204);
    	}
    	
          
        
        
    	  chart = createChart();        
         
          composite1 = new ChartComposite(parent, SWT.NONE, chart, true);
          composite1.setDisplayToolTips(false);
          composite1.setHorizontalAxisTrace(true);
          composite1.setVerticalAxisTrace(true);
          
          
           //updater =  new UpdateEc2InstanceThread(this);
          
           //updater.start();
           
           updater = Config.getInstance().getUpdater();
       	
           updater.addListener(this);
           //updater.start();
      
          
          
        //composite1 = new Composite(parent, SWT.NULL);
        //composite1.setLayout(new GridLayout(4, false));
    }

    /* (non-Javadoc)
     * @see org.eclipse.ui.IWorkbenchPart#setFocus()
     */
    public void setFocus() {
        // TODO Auto-generated method stub
    }
    
    /**
     * Cleans up all resources created by this ViewPart.
     */
    public void dispose() {
    	updater.interrupt();
        super.dispose();
    }
    
    /**
     * Creates the demo chart.
     * 
     * @return The chart.
     */
    private JFreeChart createChart() {

    	this.series = new TimeSeries("Num Ec2 Instances",Second.class);
        TimeSeriesCollection dataset = new TimeSeriesCollection(this.series);
    	 
         
         
       /* XYDataset dataset1 = createDataset("Series 1", 100.0, new Minute(), 
                200);
        */
        JFreeChart chart = ChartFactory.createTimeSeriesChart(
        		"Ec2 Running Instances", 
                "Time", 
                "Value",
            dataset, 
            true, 
            false, 
            false
        );
        
        
        final XYPlot plot = chart.getXYPlot();
        ValueAxis axis = plot.getDomainAxis();
        axis.setAutoRange(true);
        axis.setFixedAutoRange(60000.0 * 30);  // last 30 minutes
        axis = plot.getRangeAxis();
        axis.setRange(0.0, 15.0); 
   
        return chart;
    }
    

    
	public TimeSeries getSeries() {
		return series;
	}

	public void setSeries(TimeSeries series) {
		this.series = series;
	}
    
}
